using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Description:通知弹框
/// Author:哒先生
/// Date:2024年11月13日11:01:17
/// </summary>
public class NotificationBox : MonoBehaviour
{
    [Header("Header")] [SerializeField] private Transform headerArea;
    [SerializeField] private Text title;

    public int titleFontSize
    {
        set
        {
            title.fontSize = value;
        }
    }

    [Header("Content")] [SerializeField] private Transform contentArea;
    [SerializeField] private Transform horLayoutArea;
    [SerializeField] private Image horImage;
    [SerializeField] private Text horText;

    [SerializeField] private Transform verLayoutArea;
    [SerializeField] private Image verImage;
    [SerializeField] private Text verText;

    [Header("Footer")] [SerializeField] private Transform footerArea;
    [SerializeField] private HButtonBase confirmButton;
    public string confirmButtonName
    {
        set
        {
            confirmButton.transform.GetComponentInChildren<Text>().text = value;
        }
    }
    [SerializeField] private HButtonBase cancelButton;
    public string cancelButtonName
    {
        set
        {
            cancelButton.transform.GetComponentInChildren<Text>().text = value;
        }
    }
    [SerializeField] private HButtonBase alternateButton;
    public string alternateButtonName
    {
        set
        {
            alternateButton.transform.GetComponentInChildren<Text>().text = value;
        }
    }

    private Action confirmAction;
    private Action cancelAction;
    private Action alternateAction;

    /// <summary>
    /// 垂直布局显示
    /// </summary>
    /// <param name="title"></param>
    /// <param name="sprite"></param>
    /// <param name="str"></param>
    /// <param name="confirm"></param>
    /// <param name="cancel"></param>
    /// <param name="alternate"></param>
    public void ShowAsVer(string title = "", Sprite sprite = null, string str = "", Action confirm = null, Action cancel = null, Action alternate = null)
    {
        gameObject.SetActive(true);
        horLayoutArea.gameObject.SetActive(false);
        verLayoutArea.gameObject.SetActive(true); 
        
        headerArea.gameObject.SetActive( title != "");
        this.title.text = title;

        verImage.gameObject.SetActive(sprite != null);
        verImage.sprite = sprite;
        verText.gameObject.SetActive(str != "");
        verText.text = str;

        confirmButtonName = "确定";
        confirmAction = confirm;
        confirmButton.gameObject.SetActive(confirm != null);
        cancelButtonName = "取消";
        cancelAction = cancel;
        cancelButton.gameObject.SetActive(cancel != null);
        alternateAction = alternate;
        alternateButton.gameObject.SetActive(alternate != null);
    }
    
    /// <summary>
    /// 水平布局显示
    /// </summary>
    /// <param name="title"></param>
    /// <param name="sprite"></param>
    /// <param name="str"></param>
    /// <param name="confirm"></param>
    /// <param name="cancel"></param>
    /// <param name="alternate"></param>
    public void ShowAsHor(string title = "", Sprite sprite = null, string str = "", Action confirm = null, Action cancel = null, Action alternate = null)
    {
        gameObject.SetActive(true);
        horLayoutArea.gameObject.SetActive(true);
        verLayoutArea.gameObject.SetActive(false); 
        
        headerArea.gameObject.SetActive( title != "");
        this.title.text = title;

        horImage.gameObject.SetActive(sprite != null);
        horImage.sprite = sprite;
        horText.gameObject.SetActive(str != "");
        horText.text = str;

        confirmButtonName = "确定";
        confirmAction = confirm;
        confirmButton.gameObject.SetActive(confirm != null);
        cancelButtonName = "取消";
        cancelAction = cancel;
        cancelButton.gameObject.SetActive(cancel != null);
        alternateAction = alternate;
        alternateButton.gameObject.SetActive(alternate != null);
    }

    /// <summary>
    /// 设置垂直布局的图片尺寸
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void SetVerImageSizeDelta(float x ,float y)
    {
        verImage.rectTransform.sizeDelta = new Vector2(x,y);
    }
    
    /// <summary>
    /// 设置水平布局的图片尺寸
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void SetHorImageSizeDelta(float x ,float y)
    {
        horImage.rectTransform.sizeDelta = new(x,y);
    }

    /// <summary>
    /// 设置垂直布局的文字宽度和字体大小
    /// </summary>
    /// <param name="width"></param>
    /// <param name="fontSize">默认为0，为0时不改变字体大小</param>
    public void SetVerTextWidthAndFontSize(float width,int fontSize = 0)
    {
        verText.rectTransform.sizeDelta = new Vector2(width, verText.rectTransform.sizeDelta.y);
        if (fontSize != 0)
        {
            verText.fontSize = fontSize;
        }
    }
    
    /// <summary>
    /// 设置水平布局的文字宽度和字体大小
    /// </summary>
    /// <param name="width"></param>
    /// <param name="fontSize">默认为0，为0时不改变字体大小</param>
    public void SetHorTextWidthAndFontSize(float width,int fontSize = 0)
    {
        horText.rectTransform.sizeDelta = new Vector2(width, horText.rectTransform.sizeDelta.y);
        if (fontSize != 0)
        {
            horText.fontSize = fontSize;
        }
    }
    
    public void Confirm()
    {
        confirmAction?.Invoke();
        Close();
    }
    
    public void Cancel()
    {
        cancelAction?.Invoke();
        Close();
    }
    
    public void Alternate()
    {
        alternateAction?.Invoke();
        Close();
    }

    private void Close()
    {
        gameObject.SetActive(false);
    }
}